﻿using UnityEngine;
using System.Collections;

public class BoardController: MonoBehaviour {
	private Vector2 gridSize = new Vector2(15.0f, 15.0f);

	void OnMouseDown () {
		Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		getBox(worldPos);
	}

	public Vector2 getBox(Vector3 clickPos){
		// pozitia relativa la board
		Vector2 localPos = transform.InverseTransformPoint(clickPos);
		//scadem offsetul de 0.5 al plansei
		localPos += new Vector2(0.5f, 0.5f); //e intre 0 si 1
		//convertim la vector 2 ca sa fie mai usoare transformarile
		Vector2 localPos2D = localPos;
		// inmultim localpos cu grid size (scale in loc de *)
		Vector2 celPos = Vector2.Scale(localPos2D, gridSize);
		// (x=coloana, y=linia)
		Vector2 gridPos = new Vector2(Mathf.Floor(celPos.x), Mathf.Floor(celPos.y));
		return gridPos;
	}

	public Vector3 convertBoardToWorld(Vector2 gridPos){
		// move to cell middle
		gridPos += new Vector2(0.5f, 0.5f);
		// undo la inmultirea localpos to gridsize din getBox
		Vector2 localPos2D = new Vector2(gridPos.x / gridSize.x, gridPos.y / gridSize.y);
		// undo 0.5 offset
		localPos2D -= new Vector2(0.5f, 0.5f);
		//conversie la vec3 pt pos z
		Vector3 localPos = localPos2D;
		localPos.z = transform.position.z;
		// transform totul la world coordinates
		Vector3 worldPos = transform.TransformPoint(localPos);
		return worldPos;
	}

}







